//var baseUrl = 'http://192.168.110.108:8080';
var baseUrl = 'http://stl.bobzyh.top/api';
var loginUser = JSON.parse(sessionStorage.getItem('member'));

reloadMember();

$('.login').click(function(){
	var dialog = new bDialog();
	dialog.open({
		title: '会员登录',
		html: $('#login').html(),
	});
});

$('html').on('submit', '.loginForm', function(){
	var phone = $(this).find('input[name="phone"]').val();
	var password = $(this).find('input[name="password"]').val();
	
	console.log({phone, password});
	$.ajax({
		url: baseUrl + '/fore/member/login',
		data: JSON.stringify({phone, password}),
		contentType: 'application/json; charset=UTF-8',
		type: 'post',
		dataType: 'JSON',
		success(result){
			if (result.code == 0){
				sessionStorage.setItem('token', result.msg);
				sessionStorage.setItem('member', JSON.stringify(result.data));
				loginUser = result.data;
				// 刷新界面
				reloadMember();
				$('.dialog').remove();				
			} else {
				new bDialog().open({
					title: '警告',
					html: result.msg,
				});
			}
		},
	});
	
	return false;
});

function reloadMember(){
	console.log(loginUser);
	
	if (loginUser != null) {
		// 刷新界面
		$('.infos .id .info').text(loginUser.id)
		$('.infos .phone .info').text(loginUser.phone)
		$('.infos .name .info').text(loginUser.name)
		$('.infos .balance .info').text(loginUser.balance)
		$('.login').hide();
		$('.change').show();
		$('.logout').show();
	} else {
		$('.infos .id .info').text('未登录')
		$('.infos .phone .info').text('未登录')
		$('.infos .name .info').text('未登录')
		$('.infos .balance .info').text('未登录')
		$('.login').show();
		$('.change').hide();
		$('.logout').hide();
	}
}


$('.logout').click(function(){
	// 清除数据
	sessionStorage.removeItem('token');
	sessionStorage.removeItem('member');
	loginUser = null;
	reloadMember()
});
